package com.eystar.pdc.model;

import java.util.List;

import com.eystar.ext.kit.page.PageOrder;
import com.eystar.ext.plugin.tablebind.ModelExt;
import com.eystar.ext.plugin.tablebind.TableBind;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;

import cn.hutool.core.util.StrUtil;

/**
 * 角色Model
 * @author Your_Name
 *
 */
@TableBind(tableName = "t_pdc_role")
public class Role extends ModelExt<Role> {

	private static final long serialVersionUID = 1244431359124317083L;
	public final static Role dao = new Role();
	
	/**
	 * 分页查找对象
	 *
	 * @param order
	 * @param user
	 * @return
	 */
	public Page<Role> paginate(PageOrder order, Role role) {
		SqlPara sqlPara = getSqlPara("pdc.role.paginate",role);
		return paginate(order.getPageNum(), order.getPageSize(), sqlPara);
	}
	
	/**
	 * 根据roleid，递归查询所有的角色id，返回   role_id,role_id格式
	 * @param roleId
	 * @return
	 */
	public String findRoleIdsRecursion(String roleId){
		List<Role> roles = find();
		StringBuffer sb = new StringBuffer();
		roleRecursion(sb, roles, roleId);
		return StrUtil.removeSuffix(sb.toString(), ",");
	}
	
	/**
	 * 递归循环角色
	 * @param sb
	 * @param roles
	 * @param roleId
	 */
	private void roleRecursion(StringBuffer sb,List<Role> roles,String roleId){
		for (Role role : roles) {
			if (StrUtil.equals(roleId, role.getStr("id"))) {
				sb.append(roleId).append(",");
				String pid = role.getStr("role_pid");
				if (StrUtil.isNotBlank(pid) && !StrUtil.equals(pid, "0")) {
					roleRecursion(sb, roles, pid);
					return;
				}
			}
		}
	}
	
	/***
	 * 查询普通角色
	 * @return
	 */
	public List<Role> findNormalRoles(){
		return findByColumn("role_flag", 20);
	}
	
}